package edu.jhmi.tools.omssa_gui;

/**
 * @author Ravi Tharakan
 * @author Lennart Martens
 */

import java.util.*;
import java.io.*;

/**
 * This class takes care of building the omssacl process
 *
 */

@SuppressWarnings({"ObjectEqualsNull"})
public class OmssaclProcessBuilder extends OmssaguiProcessBuilder {
    
    /**
     * Constructor.
     * @param omssacl_directory directory location of omssacl.exe
     * @param spectraFile string location of spectra file to search
     * @param outputFile string location where to send omx formatted results file
     * @param missedCleavages number of missed cleavage allowed
     * @param precursorTolerance mass tolerance for precursor
     * @param fragmentTolerance mass tolerance for fragment
     * @param eValue e-value cutoff
     * @param hitList hit-list length for each
     * @param chargeLowerBound lower bound of charge to search for
     * @param chargeUpperBound upper bound of charge to search for
     * @param minChargeForMultiplyCharged minimum charge to start looking for multiply charged ions
     * @param selected_fixedmods integer array indicating fixed modifications to search
     * @param selected_varmods integer array indicating variable modifications to search
     * @param selected_enzyme integer indicating enzyme used
     * @param dbFile String with the database file to use
     */
    public OmssaclProcessBuilder(File omssacl_directory, String spectraFile, String outputFile, int missedCleavages, float precursorTolerance,
				 float fragmentTolerance, float eValue, int hitList, int chargeLowerBound, int chargeUpperBound,
				 int minChargeForMultiplyCharged, int[] selected_fixedmods, int[] selected_varmods, int selected_enzyme, String dbFile) {
        // for windows or mac
        checkOperatingSystem();
        // The database file path and name.
        File seqDBFile = new File(dbFile);
        File dbFilePath = seqDBFile.getParentFile();

        // full path to executable
        process_name_array.add(omssacl_directory.getAbsolutePath() + "/omssacl");
        // always ask for spectra included in results file
        process_name_array.add("-w");

        // check if set by user (that is, if not defaults) and then add to array
        if (fragmentTolerance != 0.8F && fragmentTolerance != -1) {
            process_name_array.add("-to");
            process_name_array.add(Float.toString(fragmentTolerance));
        }
        if (precursorTolerance != 2.0 && precursorTolerance != -1) {
            process_name_array.add("-te");
            process_name_array.add(Float.toString(precursorTolerance));
        }
        if (selected_enzyme != 0 && selected_enzyme != -1) {
            process_name_array.add("-e");
            process_name_array.add(Integer.toString(selected_enzyme));
        }
        if (chargeLowerBound != 1 && chargeLowerBound != -1) {
            process_name_array.add("-zl");
            process_name_array.add(Integer.toString(chargeLowerBound));
        }
        if (chargeUpperBound != 4 && chargeUpperBound != -1) {
            process_name_array.add("-zh");
            process_name_array.add(Integer.toString(chargeUpperBound));
        }
        if (minChargeForMultiplyCharged != 3 && minChargeForMultiplyCharged != -1) {
            process_name_array.add("-zt");
            process_name_array.add(Integer.toString(minChargeForMultiplyCharged));
        }
        if (missedCleavages != 1 && missedCleavages != -1) {
            process_name_array.add("-v");
            process_name_array.add(Integer.toString(missedCleavages));
        }
        if (hitList != 25 && hitList != -1) {
            process_name_array.add("-hl");
            process_name_array.add(Integer.toString(hitList));
        }
        if (eValue != 1 && eValue != -1) {
            process_name_array.add("-he");
            process_name_array.add(Float.toString(eValue));
        }
        StringBuffer sf = new StringBuffer();
        for (int i = 0; i < selected_fixedmods.length; i++) {
            int lSelected_fixedmod = selected_fixedmods[i];
            // Fence-post on the comma.
            if(i > 0) {
                sf.append(",");
            }
            sf.append(Integer.toString(lSelected_fixedmod));
        }
        if (!(sf.length()==0)) {
            process_name_array.add("-mf");
            process_name_array.add(sf.toString());
        }

        StringBuffer sv = new StringBuffer();
        for (int i = 0; i < selected_varmods.length; i++) {
            int lSelected_varmod = selected_varmods[i];
            // Fence-post on the comma.
            if(i > 0) {
                sv.append(",");
            }
            sv.append(Integer.toString(lSelected_varmod));
        }
        if (!(sv.length()==0)) {
            process_name_array.add("-mv");
            process_name_array.add(sv.toString());
        }
        if (!dbFile.equals(null)) {
            process_name_array.add("-d");
            process_name_array.add(seqDBFile.getName());
        }
        if (!spectraFile.equals(null)) {
            if (spectraFile.endsWith("mgf")) {
                process_name_array.add("-fm");
            } else if (spectraFile.endsWith("pkl")) {
                process_name_array.add("-fp");
            } else if (spectraFile.endsWith("dta")) {
                process_name_array.add("-f");
            }
            process_name_array.add(spectraFile);
        }
        if (!outputFile.equals(null)) {
            process_name_array.add("-ox");
            process_name_array.add(outputFile);
        }
        process_name_array.trimToSize();
// @TODO
        System.out.println(process_name_array);
        pb = new ProcessBuilder(process_name_array);
//@todo
        pb.directory(dbFilePath);
        // set error out and std out to same stream
        pb.redirectErrorStream(true);
    }
}
